﻿using Common.Exceptions;
using Microsoft.EntityFrameworkCore;
using Wallpaper.Data.IDal;
using Wallpaper.Models.DTOs.Responses.Tag;
using Wallpaper.Models.Entities;
using Wallpaper.Services.IService;

namespace Wallpaper.Services.Service
{
    public class TagService : ITagService
    {
        readonly ITagDal _tagDal;
      

        public TagService(ITagDal tagDal)
        {
            _tagDal = tagDal;
            
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="tagName"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="BusinessException"></exception>
        public async Task CreateAsync(string tagName)
        {
            // 参数验证 - 使用 ArgumentException
            if (string.IsNullOrWhiteSpace(tagName))
            {
                throw new ArgumentNullException(nameof(tagName), "标签名称不能为空");
            }

            if (tagName.Trim().Length > 10)
            {
                throw new ArgumentException("标签名称不能超过10个字符", nameof(tagName));
            }
            bool existing = await _tagDal.ExistsByNameAsync(tagName.Trim());
            if (existing)
            {
                throw new BusinessException("标签已存在");
            }
            var tag = new Tags
            {
                Id = Guid.NewGuid().ToString(),
                Name = tagName.Trim(),
                CreatedAt = DateTime.UtcNow
            };
            await _tagDal.CreateEntityAsync(tag);
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task DeleteAsync(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentNullException(nameof(id), "id名称不能为空");
            }
            var tag= await _tagDal.GetByIdAsync(id);
            if (tag == null)
            {
                throw new BusinessException("标签不存在", 404);
            }
            // 检查是否被壁纸使用
         
            if (await _tagDal.IsTagUsedAsync(id))
            {
                throw new BusinessException("标签正在被壁纸使用，无法删除", 400);
            }
             await _tagDal.DeleteEntityAsync(tag);
        }


        /// <summary>
        /// 获取前3条热门标签
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<TagResponse>> GetHotTagsAsync(int count)
        {
            var tags = await _tagDal.GetQueryable()
                .Where(t => !t.IsDeleted)
                .OrderByDescending(t => t.UsageCount)
                .Take(count)
                .Select(t => new TagResponse
                {
                    Id = t.Id,
                    Name = t.Name,
                    UsageCount = t.UsageCount,
                })
                .ToListAsync();

            return tags;
        }


        /// <summary>
        /// 获取标签
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<TagResponse>> GetListAsync()
        {
            return  await _tagDal.GetListAsync();
            
        }
    }
}
